package com.shxc.smarttrade.old.foreignfragment;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.google.gson.Gson;
import com.shxc.smarttrade.R;
import com.shxc.smarttrade.old.activity.AlphaActivity;
import com.shxc.smarttrade.old.bean.AlphaBaseInfo;
import com.shxc.smarttrade.old.constants.URLContents;
import com.shxc.smarttrade.old.utils.BaseUtils;
import com.shxc.smarttrade.old.utils.MyOkHttpUtils;
import com.shxc.smarttrade.old.utils.SpUtils;

import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;

/**
 * Created by jcdev1 on 2017/11/9.
 */

public class AlphaDataFragment extends Fragment {

    @BindView(R.id.tv_precision_rate)
    TextView tvPrecisionRate;
    @BindView(R.id.tv_total_profit)
    TextView tvTotalProfit;
    @BindView(R.id.tv_month_profit)
    TextView tvMonthProfit;
    @BindView(R.id.tv_total_trade)
    TextView tvTotalTrade;
    @BindView(R.id.tv_max_volume)
    TextView tvMaxVolume;
    @BindView(R.id.tv_profit_order)
    TextView tvProfitOrder;
    @BindView(R.id.tv_loss_order)
    TextView tvLossOrder;
    @BindView(R.id.tv_profit_max)
    TextView tvProfitMax;
    @BindView(R.id.tv_loss_max)
    TextView tvLossMax;
    @BindView(R.id.tv_average_profit)
    TextView tvAverageProfit;
    private View rootView;
    private AlphaActivity activity;

    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    AlphaBaseInfo.DataBean dataBean = (AlphaBaseInfo.DataBean) msg.obj;
                    AlphaBaseInfo.DataBean.EaInfoBean eb = dataBean.getEaInfo();
                    tvPrecisionRate.setText(BaseUtils.getDigitsData(eb.getPrecisionRate(), 2) + "%");
                    tvTotalProfit.setText(BaseUtils.dealSymbol(eb.getProfitCount()));
                    tvMonthProfit.setText(BaseUtils.getDigitsData(eb.getMonthProfitRate(), 2) + "%");
                    tvTotalTrade.setText(eb.getTraderCount() + "笔");
                    tvMaxVolume.setText(BaseUtils.getDigitsData(eb.getMaxVolume(), 2) + "手");
                    tvProfitOrder.setText(eb.getProfitTrade() + "笔");
                    tvLossOrder.setText(eb.getLossTrade() + "笔");
                    tvProfitMax.setText(BaseUtils.dealSymbol(eb.getMaxProfit()));
                    tvLossMax.setText(BaseUtils.dealSymbol(eb.getMaxLoss()));
                    tvAverageProfit.setText(BaseUtils.dealSymbol(eb.getAvgProfit()));
                    activity.setBaseData(dataBean);
                    break;
            }
        }
    };

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (rootView == null) {
            rootView = inflater.inflate(R.layout.fragment_alpha_data, null);
        }
        //缓存的rootView需要判断是否已经被加过parent， 如果有parent需要从parent删除，要不然会发生这个rootview已经有parent的错误。
        ViewGroup parent = (ViewGroup) rootView.getParent();
        if (parent != null) {
            parent.removeView(rootView);
        }
        ButterKnife.bind(this, rootView);
        initView();
        initData();
        return rootView;
    }

    private void initView() {
        activity = (AlphaActivity) getActivity();
    }

    public void initData() {
        activity.showDialog();
        String accessToken = SpUtils.getString(getActivity(), "accessToken", "");
        int userId = SpUtils.getInt(getActivity(), "userId", -1);
        int eaId = getActivity().getIntent().getIntExtra("eaId", -1);

        Map<String, Object> map = new HashMap<>();
        map.put("eaId", String.valueOf(eaId));
        map.put("accessToken", accessToken);
        map.put("userId", String.valueOf(userId));
        MyOkHttpUtils.getServiceDatas(getActivity(),map, URLContents.ALPHA_DATA_FRAGMENT_EAINFO, new MyOkHttpUtils.ServiceStatus() {
            @Override
            public void faild(Call call, Exception e, int id) {
                BaseUtils.showToast("网络异常", getActivity());
                activity.dissDialog();
            }

            @Override
            public void success(String response, int id) {
                if (response != null) {
                    Gson gson = new Gson();
                    AlphaBaseInfo abi = gson.fromJson(response, AlphaBaseInfo.class);
                    if (abi.getErr_code().equals("0")) {
                        Message msg = Message.obtain();
                        msg.what = 0;
                        msg.obj = abi.getData();
                        handler.sendMessage(msg);

                    } else
                        BaseUtils.showToast(abi.getErr_msg(), getActivity());
                }
                activity.dissDialog();
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }
}
